// @ts-ignore
// @ts-ignore
// @ts-ignore
import {
    _decorator,
    Component,
    Node, Prefab,
    instantiate,
    toRadian, Vec3, v3,
    Camera,
    RigidBody,
    Collider,
    Label,
    ICollisionEvent,
    director,
    log, Vec2
} from "cc";


import {Player} from "./Player";

const {ccclass, property} = _decorator;

const _vec3 = v3();
const _moveFactor = 0.1;
const Total = 12;

@ccclass('GameCtrl')
export class GameCtrl extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;


    @property({type: Prefab})
    pf_pickup: Prefab = null;


    @property({type: Node})
    nd_pickParent: Node = null;

    // @ts-ignore
    @property({type: Node})
    nd_player: Player = null;

    @property({type: Node})
    nd_camera: Node = null;


    // ui
    @property({type: Label})
    lb_label: Label = null;

    @property({type: Node})
    nd_winLayer: Node = null;

    //
    offset = v3();
    count = 0;
    bWin = false;

    start() {
        this.nd_winLayer.active = false;
        Vec3.subtract(this.offset, this.nd_player.position, this.nd_camera.position);

        this.playerRb = this.nd_player.getComponent(RigidBody);

        // [3]

        // 初始化一堆
        const R = 8;
        let degress = 0;
        for (let i = 0; i < Total; ++i) {
            let nd = instantiate(this.pf_pickup);
            nd.setParent(this.nd_pickParent);
            let rad = toRadian(degress);
            let x = Math.cos(rad) * R;
            let z = Math.sin(rad) * R;
            nd.position = new Vec3(x, 0.7, z);
            degress += 30;
        }

        let playerCollider = this.nd_player.getComponent(Collider);
        playerCollider.on('onTriggerEnter', this.onTriggerEnter, this);
    }

    onTriggerEnter(event: ICollisionEvent) {
        this.count++;
        this.lb_label.string = `得分:${this.count}`;
        event.otherCollider.node.active = false;

        if (this.count >= Total) {
            log("Win....");
            this.bWin = true;
            this.nd_winLayer.active = true;
        }
    }

    onMove(delta: Vec2) {
        if(this.bWin){
            return;
        }

        let x = Math.sign(delta.x);
        let z = -Math.sign(delta.y);

        // Vec3.set(_vec3,_moveFactor * x,0,z * _moveFactor);
        // Vec3.add(_vec3,this.nd_player.position,_vec3);
        // this.nd_player.position = _vec3;
        this.playerRb.applyForce(new Vec3(x * 10, 0, z * 10));
    }

    update(deltaTime: number) {
        // [4]
        Vec3.subtract(_vec3, this.nd_player.position, this.offset);
        this.nd_camera.position = _vec3;
    }

    btnReplay(){
        // this.nd_winLayer.active = false;
        // this.bWin = false;
        // this.count = 0;
        // this.lb_label.string = `得分:${this.count}`;
        director.loadScene('demo_1');
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
